-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add configuration to allow overriding the encoding method. #23
Add configuration to allow overriding the encoding method. #23
Conversation
I don't like the idea of configuration object - it's overkill in here IMO. Also, I think there should be a way to pass encoder to Urlencoded initializer. |
93d42e1
to
74ecde7
Compare
I removed the configuration in the first commit and allowed require "uri"
module HTTP
module FormData
def self.encoder
lambda do |enum|
unescaped_chars = /[^a-z0-9\-\.\_\~]/i
enum.map do |k, v|
if v.nil?
::URI::DEFAULT_PARSER.escape(k.to_s, unescaped_chars)
elsif v.respond_to?(:to_ary)
v.to_ary.map do |w|
str = ::URI::DEFAULT_PARSER.escape(k.to_s, unescaped_chars)
unless w.nil?
str << '='
str << ::URI::DEFAULT_PARSER.escape(w.to_s, unescaped_chars)
end
end.join('&')
else
str = ::URI::DEFAULT_PARSER.escape(k.to_s, unescaped_chars)
str << '='
str << ::URI::DEFAULT_PARSER.escape(v.to_s, unescaped_chars)
end
end.join('&')
end
end
end
end I'm not a huge fan of this solution though 😕 |
Re reading your comment, maybe you were just talking about exposing the encoder without wrapping it inside a HTTP::FormData.encoder = lambda do |enum|
# ...
end |
I have changed this code a bit and merged this PR as 3bf2e83 So basically I have moved away from the idea of having per-form encoder (at least for now). And exposed encoder on Urlencoded class. Meaning one can override default implementation with: HTTP::FormData::Urlencoded.encoder = lambda { |data| ... } |
Thank you! |
Great, thanks! |
Released as v2.1.1 |
## 2.1.1 (2018-06-01) * [#23](httprb/form_data#23) Allow override urlencoded form data encoder. [@FabienChaynes][]
This PR is related to #22.
It is obviously missing some specs and documentation, which I will add once we'll agree on the implementation.
It adds a
Configuration
object with an@encoding_method
variable.This variable will receive a
Proc
and will default to the::URI.encode_www_form
method. If the variable is set, theProc
should expect a single argument responding to#to_h
and return a string of the encoded hash.It'd allow users to override the method encoding data in HTTP::FormData::Urlencoded#initialize.
An example of the usage could be: